package com.example.authsystem.controller;

import com.example.authsystem.dto.UserActivityDto;
import com.example.authsystem.dto.UserStatsDto;
import com.example.authsystem.dto.LoginStatsDto;
import com.example.authsystem.service.StatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/statistics")
@Tag(name = "数据统计", description = "系统数据统计分析接口")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    @Operation(summary = "获取用户统计", description = "获取用户注册和活跃统计数据")
    @ApiResponse(responseCode = "200", description = "获取成功")
    @GetMapping("/users")
    public UserActivityDto getUserStatistics(
            @Parameter(description = "开始日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        UserActivityDto stats = statisticsService.getUserStatistics(startDate, endDate);
        return stats;
    }

    @Operation(summary = "获取登录统计", description = "获取用户登录行为统计数据")
    @ApiResponse(responseCode = "200", description = "获取成功")
    @GetMapping("/logins")
    public ResponseEntity<LoginStatsDto> getLoginStatistics(
            @Parameter(description = "开始日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @Parameter(description = "分组类型：daily/weekly/monthly") @RequestParam(defaultValue = "daily") String groupBy) {
        LoginStatsDto stats = statisticsService.getLoginStatistics();
        return ResponseEntity.ok(stats);
    }

    @Operation(summary = "生成数据报表", description = "生成指定类型的数据分析报表")
    @ApiResponse(responseCode = "200", description = "生成成功")
    @PostMapping("/reports/{reportType}")
    public ResponseEntity<Object> generateReport(
            @Parameter(description = "报表类型：user/login/system") @PathVariable LocalDate reportType,
            @Parameter(description = "开始日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        Object report = statisticsService.generateReport(reportType, startDate, String.valueOf(endDate));
        return ResponseEntity.ok(report);
    }

    // 在StatisticsController中添加以下方法

    @Operation(summary = "获取用户统计详情", description = "获取指定用户的详细统计信息")
    @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(schema = @Schema(implementation = UserStatsDto.class)))
    @GetMapping("/user/{userId}")
    public ResponseEntity<UserStatsDto> getUserStats(
            @Parameter(description = "用户ID")
            @PathVariable Long userId) {

        UserStatsDto stats = statisticsService.getUserStats(userId);
        return ResponseEntity.ok(stats);
    }

    @Operation(summary = "批量获取用户统计", description = "获取多个用户的统计信息")
    @ApiResponse(responseCode = "200", description = "获取成功")
    @PostMapping("/users/batch")
    public ResponseEntity<List<UserStatsDto>> getBatchUserStats(
            @Parameter(description = "用户ID列表")
            @RequestParam List<Long> userIds) {

        List<UserStatsDto> statsList = statisticsService.getBatchUserStats(userIds);
        return ResponseEntity.ok(statsList);
    }

}
